﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Data;
using Site_IEQ.IEQ_DAL.DsGrupoDeptoTableAdapters;

namespace Site_IEQ.IEQ_DAL
{
    public class ServerDataAcces
    {
        private string ConnectionString
        {
            get { return WebConfigurationManager.ConnectionStrings["conn_portal_ieq"].ConnectionString; }
        }

        public bool ValidateLogin(String user_name, String user_pass)
        {
            bool result = false;

            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
                try
                {
                    connection.Open();
                    string query = String.Format("SELECT * FROM IEQ_USERS WHERE USER_NAME = '{0}' AND USER_PASS = '{1}' ", user_name, user_pass);
                    SqlCommand command = new SqlCommand(query, connection);
                    SqlDataReader reader = command.ExecuteReader();
                    result = (reader.HasRows);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    connection.Close();
                }
            }

            return result;
        }

        public DataTable Select(String sqlQuery)
        {
            DataTable dt = new DataTable("dt_result");

            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
                try
                {
                    connection.Open();
                    SqlCommand command = new SqlCommand(sqlQuery, connection);
                    SqlDataAdapter da = new SqlDataAdapter(command);
                    da.Fill(dt);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    connection.Close();
                }
            }
            return dt;
        }

        public Boolean Update(String sqlQuery)
        {
            bool result = false;
            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
                try
                {
                    connection.Open();
                    SqlCommand command = new SqlCommand(sqlQuery, connection);
                    if (command.ExecuteNonQuery() > 0)
                        result = true;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    connection.Close();
                }
            }

            return result;
        }
               
    }
}